
package com.eworking.eim.util;

import java.io.File;

import android.content.Context;
import android.os.Environment;

public class PathUtil {
    
    /** 应用根目录 */
    public static String pathPrefix;
    
    public static final String historyPathName = "/chat/";
    
    /** 图片存放目录 */
    public static final String imagePathName = "/image/";
    
    /** 语音存放目录 */
    public static final String voicePathName = "/voice/";
    
    /** 视频存放目录 */
    public static final String videoPathName = "/video/";
    
    /** 网络硬盘存放目录 */
    public static final String netdiskDownloadPathName = "/netdisk/";
    
    /** 会议存放目录 */
    public static final String meetingPathName = "/meeting/";
    
    /** SD卡路径 */
    private static File storageDir = null;
    
    private static PathUtil instance = null;
    
    /** 语音文件 */
    private File voicePath = null;
    
    /** 图片文件 */
    private File imagePath = null;
    
    private File historyPath = null;
    
    /** 视频文件 */
    private File videoPath = null;
    
    public static PathUtil getInstance() {
        if (instance == null)
            instance = new PathUtil();
        return instance;
    }
    
    /**
     * 初始化客户端文件存放目录 （附件、图片、视频、语音）
     * 
     * @param entCode 企业账号
     * @param account 用户账号
     * @param context
     */
    public void initDirs(String entCode, String account, Context context) {
        String packageName = context.getPackageName();
        pathPrefix = "/Android/data/" + packageName + "/";
        this.voicePath = generateVoicePath(entCode, account, context);
        if (!(this.voicePath.exists())) {
            this.voicePath.mkdirs();
        }
        this.imagePath = generateImagePath(entCode, account, context);
        if (!(this.imagePath.exists())) {
            this.imagePath.mkdirs();
        }
        this.historyPath = generateHistoryPath(entCode, account, context);
        if (!(this.historyPath.exists())) {
            this.historyPath.mkdirs();
        }
        this.videoPath = generateVideoPath(entCode, account, context);
        if (!(this.videoPath.exists())) {
            this.videoPath.mkdirs();
        }
    }
    
    public File getImagePath() {
        return this.imagePath;
    }
    
    public File getVoicePath() {
        return this.voicePath;
    }
    
    public File getVideoPath() {
        return this.videoPath;
    }
    
    public File getHistoryPath() {
        return this.historyPath;
    }
    
    /**
     * SD卡目录
     * 
     * @param context
     * @return
     */
    private static File getStorageDir(Context context) {
        if (storageDir == null) {
            File localFile = Environment.getExternalStorageDirectory();
            if (localFile.exists())
                return localFile;
            storageDir = context.getFilesDir();
        }
        return storageDir;
    }
    
    /**
     * 生成图片存储目录
     * 
     * @param entCode 企业代码
     * @param account 用户账号
     * @param context
     * @return
     */
    private static File generateImagePath(String entCode, String account, Context context) {
        String str = null;
        if (entCode == null)
            str = pathPrefix + account + "/image/";
        else
            str = pathPrefix + entCode + "/" + account + "/image/";
        return new File(getStorageDir(context), str);
    }
    
    /**
     * 语音存放目录
     * 
     * @param entCode 企业代码
     * @param account 用户账号
     * @param context
     * @return
     */
    private static File generateVoicePath(String entCode, String account, Context context) {
        String str = null;
        if (entCode == null)
            str = pathPrefix + account + "/voice/";
        else
            str = pathPrefix + entCode + "/" + account + "/voice/";
        return new File(getStorageDir(context), str);
    }
    
    /**
     * 视频存放目录
     * 
     * @param entCode 企业代码
     * @param account 用户账号
     * @param context
     * @return
     */
    private static File generateVideoPath(String entCode, String account, Context context) {
        String str = null;
        if (entCode == null)
            str = pathPrefix + account + "/video/";
        else
            str = pathPrefix + entCode + "/" + account + "/video/";
        return new File(getStorageDir(context), str);
    }
    
    /**
     * @param entCode
     * @param account
     * @param context
     * @return
     */
    private static File generateHistoryPath(String entCode, String account, Context context) {
        String str = null;
        if (entCode == null)
            str = pathPrefix + account + "/chat/";
        else
            str = pathPrefix + entCode + "/" + account + "/chat/";
        return new File(getStorageDir(context), str);
    }
    
    /**
     * @param paramFile 临时文件
     * @return
     */
    public static File getTempPath(File paramFile) {
        File localFile = new File(paramFile.getAbsoluteFile() + ".tmp");
        return localFile;
    }
}
